package com.hotel.config.rabbitmq.customer;

import com.hotel.config.rabbitmq.config.CommentReplyConfig;
import com.hotel.config.rabbitmq.config.RedisUnReadWebSocketConfig;
import com.hotel.config.websocket.WebSocketProcess;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CommentReplyConsumer {

    @Autowired
    private WebSocketProcess webSocketProcess;

    @RabbitHandler
    @RabbitListener(queues = CommentReplyConfig.COMMENT_REPLY_QUEUE)
    public void getRedisUnReadWebSocketConsumer(Channel channel, Message message){
        //成功存入消息队列
        System.out.println("成功将消息存入消息队列..."+new String(message.getBody()));
        //发送websocket
        try {
            webSocketProcess.sendAllMessage(new String(message.getBody()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //消息确认
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = CommentReplyConfig.DEAD_COMMENT_REPLY_QUEUE)
    public void getDeadRedisUnReadWebSocketConsumer(Channel channel, Message message){
        System.out.println("消息进入死信队列..."+new String(message.getBody()));
        //成功存入消息队列
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
